package com.example.memo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.memo.db.UserHelper;
import com.example.memo.entity.UserInfo;

public class Login extends AppCompatActivity implements View.OnClickListener{
    private EditText et_account;
    private EditText et_password;
    private Button btn_login;
    private CheckBox remember;
    private TextView toRegister;
    private UserHelper userHelper;
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        initData();
    }

    private void initView(){
        et_account = findViewById(R.id.account);
        et_password = findViewById(R.id.password);
        btn_login = findViewById(R.id.login);
        remember = findViewById(R.id.remember);
        toRegister = findViewById(R.id.toRegister);
        userHelper = new UserHelper(this);

        btn_login.setOnClickListener(this);
        toRegister.setOnClickListener(this);
    }

    private void initData() {
        sharedPreferences = getSharedPreferences("record",MODE_PRIVATE);
        editor = sharedPreferences.edit();
        boolean checked = sharedPreferences.getBoolean("checked",false);
        if(checked){
            String account = sharedPreferences.getString("account","");
            String password = sharedPreferences.getString("password","");

            et_account.setText(account);
            et_password.setText(password);
            remember.setChecked(true);
        }
    }

    @Override
    public void onClick(View view) {
        if(view == btn_login){
            String account = et_account.getText().toString();
            String password = et_password.getText().toString();

            //账号密码判断
            if(TextUtils.isEmpty(account) || TextUtils.isEmpty(password)){
                Toast.makeText(Login.this,"账号和密码不能为空！",Toast.LENGTH_LONG).show();
            }else{
                UserInfo userInfo = userHelper.login(account);
                if(userInfo != null){
                    if(userInfo.getPassword().equals(password) && userInfo.getAccount().equals(account)){
                        UserInfo.setUserInfo(userInfo);
                        if(remember.isChecked()){
                            editor.putString("account",userInfo.getAccount());
                            editor.putString("password",userInfo.getPassword());
                            editor.putBoolean("checked",true);
                            editor.apply();
                        }else {
                            editor.putBoolean("checked",false);
                            editor.apply();
                        }
                        startActivity(new Intent(Login.this,MainActivity.class));
                        finish();
                    }else{
                        Toast.makeText(Login.this,"登陆失败，密码错误！",Toast.LENGTH_LONG).show();
                    }
                }else{
                    Toast.makeText(Login.this,"登陆失败，不存在该用户！",Toast.LENGTH_LONG).show();
                }
            }
        }else if(view == toRegister){
            startActivity(new Intent(Login.this, Register.class));
        }
    }
}